/* $RCSFile: LangListDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/LangListDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.Globalization;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using AddonNice.UI;
using AddonNice.Diagnostics;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Helpers;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// LangListDataType
	/// </summary>
	public class LangListDataType : ListDataType
	{
		public LangListDataType()
		{
			InnerDataType = PropertiesDataType.List;
		}

		protected override void InitializeComponents()
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LangListDataType InitializeComponents BEG ReadOnly: {0}",ReadOnly),DataTypeTraceSwitch.Sw.Info);
			//Language grid
            innerControl                                =   new LangGridView();
            if ( ReadOnly )
                ((LangGridView)innerControl).Enabled    =   false;
		}
		
		public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				//be sure it is a valid value
				LanguageCultureCollection myLanguageCultureCollection = (LanguageCultureCollection) TypeDescriptor.GetConverter(typeof(LanguageCultureCollection)).ConvertTo(value, typeof(LanguageCultureCollection));
				innerValue = (string) TypeDescriptor.GetConverter(typeof(LanguageCultureCollection)).ConvertToString(myLanguageCultureCollection);
			}
		}		

        public override void DataBind()
        {
			try
			{
                LangGridView languagesGrid = (LangGridView)innerControl;
				LanguageCultureCollection myLanguageCultureCollection = (LanguageCultureCollection) TypeDescriptor.GetConverter(typeof(LanguageCultureCollection)).ConvertTo(innerValue, typeof(LanguageCultureCollection));
				languagesGrid.DataSource = myLanguageCultureCollection;
				languagesGrid.DataBind();
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]LangListDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
        }

        /// <summary>
        /// Update value from control: used by eventhandler after a control change event triggered
        /// </summary>
        public override  string ReadDataFromEditControl()
		{
            Control Ctl =   EditControl;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LangListDataType ReadDataFromEditControl BEG Control is null: {0}, Val: {1}.", (Ctl == null), Val ), DataTypeTraceSwitch.Sw.Info);
            if ( Ctl ==   null )
                return Val;
            if ( Ctl is LangGridView)
            {
                LangGridView languagesGrid = (LangGridView)Ctl;
				languagesGrid.UpdateRows();
				innerValue = (string) TypeDescriptor.GetConverter(typeof(LanguageCultureCollection)).ConvertToString(languagesGrid.DataSource);
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LangListDataType EditControl UseRadControl Set Value: {0}", innerValue),DataTypeTraceSwitch.Sw.Info);
            }
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]LangListDataType ReadDataFromEditControl END InnerValue: {0}.", innerValue), DataTypeTraceSwitch.Sw.Info);
            return Val;
        }

        public override Control EditControl
		{
			get
			{
				if (innerControl == null)
					InitializeComponents();
				return innerControl;
			}
			set
			{
                if (value is LangGridView )
				{
					//Update value from control
                    LangGridView languagesGrid  = (LangGridView)value;
					languagesGrid.UpdateRows();
					innerValue                  = (string) TypeDescriptor.GetConverter(typeof(LanguageCultureCollection)).ConvertToString(languagesGrid.DataSource);
					innerControl                = languagesGrid;
                    return;
				}
				string msg  =   string.Format("[E]LangListDataType SET EditControl: {0}, not LangGridView",value.GetType());
				if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(msg,DataTypeTraceSwitch.Sw.Info);
				throw new ArgumentException(msg);
			}
		}

		public override string Description
		{
			get
			{
				return "Language List";
			}
		}
	}
}